/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.editpart;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPart;
import org.eclipse.ui.views.properties.IPropertySource;
import org.eclipse.uml2.uml.ProfileApplication;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphEdgeEditPart;
import com.cea.papyrus.core.editpart.StereotypeDisplayEditPart;
import com.cea.papyrus.core.figures.MiddleEndpointLocator;
import com.cea.papyrus.diagram.classdiagram.figures.DependencyFigure;
import com.cea.papyrus.diagraminterchange2.di2.DiagramElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * 
 */
public class ProfileApplicationEditPart extends GraphEdgeEditPart 
implements PropertyChangeListener {

	/**
	 * 
	 */
	protected StereotypeDisplayEditPart theStereotypeLabel; 
	
	/**
	 * 
	 */
	protected boolean selected;
	
	/**
	 * 
	 */
	protected IPropertySource propertySource = null;

	/**
	 * 
	 * 
	 * @param graphEdge 
	 * @param modelManager 
	 */
	public ProfileApplicationEditPart(GraphEdge graphEdge,ModelManager modelManager) {
		super(graphEdge,modelManager);
		theStereotypeLabel=new StereotypeDisplayEditPart((GraphNode)getGraphEdge().getContained().get(0), this.getUmlElement(), modelManager, this);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	/**
	 * 
	 */
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#propertyChange(java.beans.PropertyChangeEvent)
	 */
	/**
	 * 
	 * 
	 * @param event 
	 */
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		//refreshVisuals();
		refreshBendpoints();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected IFigure createFigure() {
		return new DependencyFigure();

	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#getUmlElement()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public ProfileApplication getUmlElement() {
		return (ProfileApplication)((Uml1SemanticModelBridge)(getGraphEdge()).getSemanticModel()).getElement();
	}

	/**
	 * Returns the children of this through the model.
	 * 
	 * @return Children of this as a List.
	 */
	@Override
	protected List<DiagramElement> getModelChildren() {
		ArrayList<DiagramElement> contained= new ArrayList<DiagramElement>();
		Iterator<DiagramElement> it=getGraphEdge().getContained().iterator();
		while (it.hasNext()){
			GraphElement ge=(GraphElement)it.next(); //getOnly graphNode
			if (!(ge instanceof GraphEdge)){
				contained.add(ge);
			}
		}

		return contained;

	}

	/* (non-Javadoc)
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createChild(java.lang.Object)
	 */
	/**
	 * 
	 * 
	 * @param model 
	 * 
	 * @return 
	 */
	@Override
	protected EditPart createChild(Object model)
	{
		if( model instanceof GraphNode)
		{
			//first graphNode= name of the stereotype
			if( ((GraphNode)model) == ((GraphNode)getGraphEdge().getContained().get(0)) ) {
				return theStereotypeLabel;
			}

			throw new RuntimeException("unknown model list");
		}

		return super.createChild(model);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#refreshVisuals()
	 */
	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		super.refreshVisuals();
		theStereotypeLabel.refresh();

		//calculate middle point of the stereotype label
		org.eclipse.draw2d.geometry.Point middleLabel =new org.eclipse.draw2d.geometry.Point(
				theStereotypeLabel.getGraphNode().getPosition().x-((Label) theStereotypeLabel.getFigure()).getPreferredSize().width/2,
				theStereotypeLabel.getGraphNode().getPosition().y-((Label) theStereotypeLabel.getFigure()).getPreferredSize().height/2
		);
		//caluclate middle point of the link
		MiddleEndpointLocator lo =new MiddleEndpointLocator ((DependencyFigure)this.getFigure(),
				middleLabel);

		IFigure f=theStereotypeLabel.getFigure();
		f.setLocation(theStereotypeLabel.getGraphNode().getPosition());//set lael a the good position
		if (theStereotypeLabel.isVisible()){
			this.getContentPane().getLayoutManager().setConstraint(theStereotypeLabel.getFigure(),lo);
		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#refresh()
	 */
	/**
	 * 
	 */
	@Override
	public void refresh() {
		super.refresh();
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.GraphEdgeEditPart#getAdapter(java.lang.Class)
	 */
	/**
	 * 
	 * 
	 * @param adapter 
	 * 
	 * @return 
	 */
	@Override
	public Object getAdapter(Class adapter) {
		/* override the default behavior defined in AbstractEditPart
		 * which would expect the model to be a property sourced. 
		 * instead the editpart can provide a property source
		 */
		if (IPropertySource.class == adapter) {
			return getPropertySource();
		}
		return super.getAdapter(adapter);
	}
}